import axios from 'axios'

let getSearchBrand = function (
    {
        currentPage = '2',
        search_type = '2l',
        activeId = '',
        orderBy = 'num',
        brand_id = '',
        brand_name = '',
        seo_en_brand_name = '',
        full_spell_category_path = '',
        kword = '',
        topicId = '',
        goBack = 'http://m.yougou.com/',
        ref = 'cate',
        cate_brand_id = ''

    }) {

    return new Promise(res => {
        let data = {
            currentPage,
            search_type,
            activeId,
            orderBy,
            brand_id,
            brand_name,
            seo_en_brand_name,
            full_spell_category_path,
            kword,
            topicId,
            goBack,
            ref,
            cate_brand_id
        }
        axios({
            url: '/api/search.sc',
            method: 'post',
            data,
            transformRequest: [function (data) {
                let ret = ''
                for (let it in data) {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
                return ret
            }],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(data => res(data.data))
    })

}

let getSearchRegionTopicId = function (
    {
        currentPage = '',
        search_type = '2l',
        activeId = '',
        orderBy = '',
        brand_id = '',
        brand_name = '',
        seo_en_brand_name = '',
        full_spell_category_path = '',
        kword = '',
        topicId = '',
        goBack = 'http://m.yougou.com/',
        first_category_no = '',
        filterJson = ''
    }) {

    return new Promise(res => {
        let data = {
            currentPage,
            search_type,
            activeId,
            orderBy,
            brand_id,
            brand_name,
            seo_en_brand_name,
            full_spell_category_path,
            kword,
            topicId,
            goBack,
            first_category_no,
            filterJson
        }
        axios({
            url: '/api/search.sc',
            method: 'post',
            data,
            transformRequest: [function (data) {
                let ret = ''
                for (let it in data) {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
                return ret
            }],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(data => res(data.data))
    })

}

let getHomeDataList = function (page) {
    return new Promise(res => {
        axios.get('/eg/homedatalist.php', {
            params: { page }
        }).then(a => res(a.data))
    })
}

let getClassification = function (id = '0b32ad4f00d24ce5a7ab06ba7f65277b') {
    return new Promise(res => {
        let data = { id }
        axios({
            url: '/api/getClassifies.sc',
            method: 'post',
            data,
            transformRequest: [function (data) {
                let ret = ''
                for (let it in data) {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
                return ret
            }],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(data => res(data.data))
    })
}

let getKeyword = function (kword) {
    return new Promise(res => {
        axios.post('/api/suggWord.sc?kword=' + kword).then(a => res(a.data))
    })
}

let getDetails = function(url) {
    return new Promise(res => {
        axios.get("/api/" + url).then(data => res(data.data))
    })
}

export {
    getSearchBrand,
    getHomeDataList,
    getClassification,
    getSearchRegionTopicId,
    getKeyword,
    getDetails
}